/**
 * Author@ Cheng Feitian
 * Date: 2021-4-4
 * Description: 实现计算器
 */

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
const int maxn = 1024;

//分别利用普通写法和多肽技术实现计算器

//普通写法
class Calculator
{
public:
    int m_Num1;
    int m_Num2;

    int getResult(string op)
    {
        int sum;
        if (op == "+")
        {
            sum = m_Num1 + m_Num2;
        }
        else if (op == "-")
        {
            sum = m_Num1 - m_Num2;
        }
        else if (op == "*")
        {
            sum = m_Num1 * m_Num2;
        }
        else
        {
            sum = m_Num1 / m_Num2;
        }
        return sum;
    }
};

void test01()
{

    //创建计算机对象
    Calculator c;
    c.m_Num1 = 10;
    c.m_Num2 = 10;

    cout << c.m_Num1 << " + " << c.m_Num2 << " = " << c.getResult("+") << endl;
    cout << c.m_Num1 << " - " << c.m_Num2 << " = " << c.getResult("-") << endl;
    cout << c.m_Num1 << " * " << c.m_Num2 << " = " << c.getResult("*") << endl;
    cout << c.m_Num1 << " / " << c.m_Num2 << " = " << c.getResult("/") << endl;
}

//利用多态实现计算器
//多态好处
//1.组织结构清晰
//2.可读性强
//3.对于前期和后期扩展以及维护性高

//实现计算器抽象类

class AbstractCalculator
{
public:
    int m_Num1;
    int m_Num2;

    virtual int getResult() { return 0; }
};

//加法计算器类
class AddCaculator : public AbstractCalculator
{
public:
    int getResult()
    {
        return m_Num1 + m_Num2;
    }
};

//减法计算器类
class SubCaculator : public AbstractCalculator
{
public:
    int getResult()
    {
        return m_Num1 - m_Num2;
    }
};

//乘法计算器类
class MulCaculator : public AbstractCalculator
{
public:
    int getResult()
    {
        return m_Num1 * m_Num2;
    }
};

//除法计算器类
class DivCaculator : public AbstractCalculator
{
public:
    int getResult()
    {
        return m_Num1 / m_Num2;
    }
};

void test02(){
    //多态使用条件
    //父类指针或者引用指向子类对象
    AbstractCalculator * abc = new AddCaculator;
    abc->m_Num1 = 10;
    abc->m_Num2 = 10;
    cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;
    delete abc;

    abc = new SubCaculator;
    abc->m_Num1 = 10;
    abc->m_Num2 = 10;
    cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;
    delete abc;

    abc = new MulCaculator;
    abc->m_Num1 = 10;
    abc->m_Num2 = 10;
    cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;
    delete abc;

    abc = new DivCaculator;
    abc->m_Num1 = 10;
    abc->m_Num2 = 10;
    cout << abc->m_Num1 << " / " << abc->m_Num2 << " = " << abc->getResult() << endl;
    delete abc;
}

int main()
{
    // test01();
    test02();
    return 0;
}
